setOutputNames <- function(out){

          names(out)     <- c("Years","Total Fishing mortality","Total biomass no Fishing","SSB no fishing","Total biomass",
                              "SSB in the future under scenario 1","SSB in the future under scenario 2","SSB in the future under scenario 3",
                              "SSB in the future under scenario 4","SSB in the future under scenario 5","Catch in the future under scenario 1",
                              "Catch in the future under scenario 2","Catch in the future under scenario 3","Catch in the future under scenario 4",
                              "Catch in the future under scenario 5","SSB","Recruitment","Numbers at age" ,"F at age fishery 1","F at age fishery 2",
                              "F at age fishery 3","F at age fishery 4","Fishery names","Indices names","Observations at age survey 1",
                              "Observations at age survey 2","Observations at age survey 3","Observations at age survey 4","Observations at age survey 5",
                              "Observations at age survey 6","Observations at age survey 7","Observations at age survey 8","Observations at age survey 9",
                              "Survey catchabilities","Proportions at age fishery 1 observed","Proportions at age fishery 2 observed","Proportions at age fishery 3 observed",
                              "Proportions at age fishery 4 observed","Proportions at age fishery 1 predicted","Proportions at age fishery 2 predicted","Proportions at age fishery 3 predicted",
                              "Proportions at age fishery 4 predicted","Proportions at age survey 1 observed","Proportions at age survey 4 observed",
                              "Proportions at age survey 1 predicted","Proportions at age survey 4 predicted","Total catch fishery 1 observed","Total catch fishery 1 predicted",
                              "Total catch fishery 2 observed","Total catch fishery 2 predicted","Total catch fishery 3 observed","Total catch fishery 3 predicted",
                              "Total catch fishery 4 observed","Total catch fishery 4 predicted","F_fsh_1","F_fsh_2","F_fsh_3","F_fsh_4","Selectivity fishery 1",
                              "Selectivity fishery 2","Selectivity fishery 3","Selectivity fishery 4","Selectivity survey 1","Selectivity survey 2","Selectivity survey 3",
                              "Selectivity survey 4","Selectivity survey 5","Selectivity survey 6","Selectivity survey 7","Selectivity survey 8","Selectivity survey 9",
                              "Stock recruitment","Stock recruitment curve","Likelihood composition","Likelihood composition names","Sel_Fshry_1","Sel_Fshry_2",
                              "Sel_Fshry_3","Sel_Fshry_4","Survey_Index_1","Survey_Index_2","Survey_Index_3","Survey_Index_4","Survey_Index_5","Survey_Index_6","Survey_Index_7",
                              "Survey_Index_8","Survey_Index_9","Age_Survey_1","Age_Survey_2","Age_Survey_3","Age_Survey_4","Age_Survey_5","Age_Survey_6","Age_Survey_7",
                              "Age_Survey_8","Age_Survey_9","Sel_Survey_1","Sel_Survey_2","Sel_Survey_3","Sel_Survey_4","Sel_Survey_5","Sel_Survey_6","Sel_Survey_7","Sel_Survey_8","Sel_Survey_9",
                              "Recruitment penalty","F penalty","Survey 1 catchability penalty","Survey 1 catchability power function","Survey 2 catchability penalty","Survey 2 catchability power function",
                              "Survey 3 catchability penalty","Survey 3 catchability power function","Survey 4 catchability penalty","Survey 4 catchability power function",
                              "Survey 5 catchability penalty","Survey 5 catchability power function","Survey 6 catchability penalty","Survey 6 catchability power function",
                              "Survey 7 catchability penalty","Survey 7 catchability power function","Survey 8 catchability penalty","Survey 8 catchability power function",
                              "Survey 9 catchability penalty","Survey 9 catchability power function","Natural mortality","Steepness of recruitment","Sigma recruitment","Number of parameters estimated",
                              "Steepness prior","Sigma recruitment prior","Rec_estimated_in_styr_endyr","SR_Curve_fit__in_styr_endyr","Model_styr_endyr","Natural mortality prior","q prior",
                              "q power prior","cv catch biomass","Projection year range","Fsh_sel_opt_fish","Survey_Sel_Opt_Survey","Phase_survey_Sel_Coffs","Fishery selectivity ages",
                              "Survey selectivity ages","Phase_for_age_spec_fishery","Phase_for_logistic_fishery","Phase_for_dble_logistic_fishery","Phase_for_age_spec_survey","Phase_for_logistic_survey",
                              "Phase_for_dble_logistic_srvy","EffN_Fsh_1","EffN_Fsh_2","EffN_Fsh_3","EffN_Fsh_4","C_fsh_1","C_fsh_2","C_fsh_3","C_fsh_4","Weight at age in the population",
                              "Maturity at age","Weight at age in fishery 1","Weight at age in fishery 2","Weight at age in fishery 3","Weight at age in fishery 4",
                              "Weight at age in survey 1","Weight at age in survey 2","Weight at age in survey 3","Weight at age in survey 4","Weight at age in survey 5",
                              "Weight at age in survey 6","Weight at age in survey 7","Weight at age in survey 8","Weight at age in survey 9","EffN_Survey_1","EffN_Survey_4")
                    return(out)}
                    
readYPR     <- function(fileName){
                 jjm.ypr            <- read.table(fileName,sep=" ",skip=4,header=T,fill=T);
                 jjm.ypr[1,]        <- jjm.ypr[1,c(1,8,2,3,4,5,6,7)]
                 colnames(jjm.ypr)  <- c("F","X","SSB","Yld","Recruit","SPR","B","X2");
                 jjm.ypr            <- jjm.ypr[,-grep("X",colnames(jjm.ypr))]
              return(jjm.ypr)}
              
              
an          <- function(x){return(as.numeric(x))}
ac          <- function(x){return(as.character(x))}
anf         <- function(x){return(as.numeric(as.character(x)))}
              
#-Code to read in final data
read.dat <- function(iFilename,iPath){
                ###-Read in the raw datafile-###
                res1      <- scan(file=paste(iPath,iFilename,sep=""),what='numeric', quiet=TRUE,sep="\n",comment.char="#",allowEscapes=T)
                res1      <- strsplit(res1,"\t")
                #res1      <- read.table(file=paste(iPath,iFilename,sep=""),header=F,comment.char="#",fill=T,sep="\t")
                ###-Prepare all seperate sets-###
                cols <- list(years        =matrix(NA,ncol=2,nrow=1 ,dimnames=list("years",c("first year","last year"))),
                             ages         =matrix(NA,ncol=2,nrow=1 ,dimnames=list("age",c("age recruit","oldest age"))),
                             Fnum         =numeric(),
                             Fnumyears    =matrix(NA,ncol=4,nrow=1,dimnames=list("years",c("Fyears1","Fyears2","Fyears3","Fyears4"))),
                             Fnames       =list(),
                             Fcaton       =matrix(NA,ncol=4,nrow=42,dimnames=list(years=1970:2011,c("fishery1","fishery2","fishery3","fishery4"))),
                             Fcatonerr    =matrix(NA,ncol=4,nrow=42,dimnames=list(years=1970:2011,c("fishery1","fishery2","fishery3","fishery4"))),
                             Fyears       =matrix(NA,ncol=4,nrow=42,dimnames=list(years=1970:2011,c("fishery1","fishery2","fishery3","fishery4"))),
                             Fagesample   =matrix(NA,ncol=4,nrow=42,dimnames=list(years=1970:2011,c("fishery1","fishery2","fishery3","fishery4"))),
                             Fagecomp     =array (NA,dim=c(42,11,4),dimnames=list(years=1970:2011,age=2:12,c("fishery1","fishery2","fishery3","fishery4"))),
                             Fwtatage     =array (NA,dim=c(42,11,4),dimnames=list(years=1970:2011,age=2:12,c("fishery1","fishery2","fishery3","fishery4"))),
                             Inum         =numeric(),
                             Inames       =list(),
                             Inumyears    =matrix(NA,ncol=9,nrow=1,dimnames=list("years",c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Iyears       =matrix(NA,ncol=9,nrow=42,dimnames=list(years=1970:2011,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Imonths      =matrix(NA,ncol=9,nrow=1,dimnames=list("month",c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Index        =matrix(NA,ncol=9,nrow=42,dimnames=list(years=1970:2011,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Indexerr     =matrix(NA,ncol=9,nrow=42,dimnames=list(years=1970:2011,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Inumageyears =matrix(NA,ncol=9,nrow=1,dimnames=list("years",c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Iyearsage    =matrix(NA,ncol=9,nrow=42,dimnames=list(years=1970:2011,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Iagesample   =matrix(NA,ncol=9,nrow=42,dimnames=list(years=1970:2011,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Ipropage     =array (NA,dim=c(42,11,9),dimnames=list(years=1970:2011,age=2:12,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Iwtatage     =array (NA,dim=c(42,11,9),dimnames=list(years=1970:2011,age=2:12,c("index1","index2","index3","index4","index5","index6","index7","index8","index9"))),
                             Pwtatage     =matrix(NA,ncol=1,nrow=11,dimnames=list(age=2:12,"weight")),
                             Pmatatage    =matrix(NA,ncol=1,nrow=11,dimnames=list(age=2:12,"maturity")),
                             Pspwn        =numeric(),
                             Pageerr      =matrix(NA,ncol=11,nrow=11,dimnames=list(age=2:12,age=2:12)))

                ###-Fill cols with data from res1-###

                #-Common data
                cols$years[]                          <- an(unlist(res1[1:2]))
                cols$ages[]                           <- an(unlist(res1[3:4]))

                #-Fisheries data
                cols$Fnum                             <- na.omit(an(unlist(res1[5])))
                cols$Fnumyears[]                      <- na.omit(an(unlist(res1[15:18])))
                cols$Fnames                           <- strsplit(unlist(res1[6]),"%")[[1]]
                cols$Fcaton[]                         <- matrix(na.omit(an(unlist(res1[7:10]))),ncol=4,nrow=42)
                cols$Fcatonerr[]                      <- matrix(na.omit(an(unlist(res1[11:14]))),ncol=4,nrow=42)

                      #-obtain years and their positions over the whole year range
                      Fyears1         <- c(na.omit(an(res1[[19]]))); wFyears1 <- pmatch(Fyears1,cols$years[1]:cols$years[2])
                      Fyears2         <- c(na.omit(an(res1[[20]]))); wFyears2 <- pmatch(Fyears2,cols$years[1]:cols$years[2])
                      Fyears3         <- c(na.omit(an(res1[[21]]))); wFyears3 <- pmatch(Fyears3,cols$years[1]:cols$years[2])
                      Fyears4         <- c(na.omit(an(res1[[22]]))); wFyears4 <- pmatch(Fyears4,cols$years[1]:cols$years[2])

                cols$Fyears[wFyears1,"fishery1"]      <- Fyears1
                cols$Fyears[wFyears2,"fishery2"]      <- Fyears2
                cols$Fyears[wFyears3,"fishery3"]      <- Fyears3
                cols$Fyears[wFyears4,"fishery4"]      <- Fyears4
                cols$Fagesample[wFyears1,"fishery1"]  <- na.omit(an(unlist(res1[23])));
                cols$Fagesample[wFyears2,"fishery2"]  <- na.omit(an(unlist(res1[24])));
                cols$Fagesample[wFyears3,"fishery3"]  <- na.omit(an(unlist(res1[25])));
                cols$Fagesample[wFyears4,"fishery4"]  <- na.omit(an(unlist(res1[26])));
                cols$Fagecomp[wFyears1,,"fishery1"]   <- matrix(na.omit(an(unlist(res1[27:(26+length(wFyears1))]))),ncol=11,
                                                                nrow=length(wFyears1),byrow=T)
                cols$Fagecomp[wFyears2,,"fishery2"]   <- matrix(na.omit(an(unlist(res1[63:(62+length(wFyears2))]))),ncol=11,
                                                                nrow=length(wFyears2),byrow=T)
                cols$Fagecomp[wFyears3,,"fishery3"]   <- matrix(na.omit(an(unlist(res1[99:(98+length(wFyears3))]))),ncol=11,
                                                                nrow=length(wFyears3),byrow=T)
                cols$Fagecomp[wFyears4,,"fishery4"]   <- matrix(na.omit(an(unlist(res1[129:(128+length(wFyears4))]))),ncol=11,
                                                                nrow=length(wFyears4),byrow=T)
                cols$Fwtatage[]                       <- array(matrix(na.omit(an(unlist(res1[145:186]))),ncol=11,
                                                               nrow=42,byrow=T),dim=c(42,11,4))

                #-Indices data
                cols$Inum                             <- na.omit(an(res1[[317]]))
                cols$Inames                           <- strsplit(res1[[318]],"%")[[1]]
                cols$Inumyears[]                      <- na.omit(an(unlist(res1[319:(318+cols$Inum)])))

                      #-obtain years and their positions over the whole year range
                      Iyears1         <- na.omit(an(res1[[328]]));  wIyears1 <- pmatch(Iyears1,cols$years[1]:cols$years[2])
                      Iyears2         <- na.omit(an(res1[[329]]));  wIyears2 <- pmatch(Iyears2,cols$years[1]:cols$years[2])
                      Iyears3         <- na.omit(an(res1[[330]]));  wIyears3 <- pmatch(Iyears3,cols$years[1]:cols$years[2])
                      Iyears4         <- na.omit(an(res1[[331]]));  wIyears4 <- pmatch(Iyears4,cols$years[1]:cols$years[2])
                      Iyears5         <- na.omit(an(res1[[332]]));  wIyears5 <- pmatch(Iyears5,cols$years[1]:cols$years[2])
                      Iyears6         <- na.omit(an(res1[[333]]));  wIyears6 <- pmatch(Iyears6,cols$years[1]:cols$years[2])
                      Iyears7         <- na.omit(an(res1[[334]]));  wIyears7 <- pmatch(Iyears7,cols$years[1]:cols$years[2])
                      Iyears8         <- na.omit(an(res1[[335]]));  wIyears8 <- pmatch(Iyears8,cols$years[1]:cols$years[2])
                      Iyears9         <- na.omit(an(res1[[336]]));  wIyears9 <- pmatch(Iyears9,cols$years[1]:cols$years[2])

                cols$Iyears[wIyears1,"index1"]        <- Iyears1
                cols$Iyears[wIyears2,"index2"]        <- Iyears2
                cols$Iyears[wIyears3,"index3"]        <- Iyears3
                cols$Iyears[wIyears4,"index4"]        <- Iyears4
                cols$Iyears[wIyears5,"index5"]        <- Iyears5
                cols$Iyears[wIyears6,"index6"]        <- Iyears6
                cols$Iyears[wIyears7,"index7"]        <- Iyears7
                cols$Iyears[wIyears8,"index8"]        <- Iyears8
                cols$Iyears[wIyears9,"index9"]        <- Iyears9
                cols$Imonths[]                        <- na.omit(an(unlist(res1[337:(336+cols$Inum)])))
                cols$Index[wIyears1,"index1"]         <- na.omit(an(res1[[346]]))
                cols$Index[wIyears2,"index2"]         <- na.omit(an(res1[[347]]))
                cols$Index[wIyears3,"index3"]         <- na.omit(an(res1[[348]]))
                cols$Index[wIyears4,"index4"]         <- na.omit(an(res1[[349]]))
                cols$Index[wIyears5,"index5"]         <- na.omit(an(res1[[350]]))
                cols$Index[wIyears6,"index6"]         <- na.omit(an(res1[[351]]))
                cols$Index[wIyears7,"index7"]         <- na.omit(an(res1[[352]]))
                cols$Index[wIyears8,"index8"]         <- na.omit(an(res1[[353]]))
                cols$Index[wIyears9,"index9"]         <- na.omit(an(res1[[354]]))
                cols$Indexerr[wIyears1,"index1"]      <- na.omit(an(res1[[355]]))
                cols$Indexerr[wIyears2,"index2"]      <- na.omit(an(res1[[356]]))
                cols$Indexerr[wIyears3,"index3"]      <- na.omit(an(res1[[357]]))
                cols$Indexerr[wIyears4,"index4"]      <- na.omit(an(res1[[358]]))
                cols$Indexerr[wIyears5,"index5"]      <- na.omit(an(res1[[359]]))
                cols$Indexerr[wIyears6,"index6"]      <- na.omit(an(res1[[360]]))
                cols$Indexerr[wIyears7,"index7"]      <- na.omit(an(res1[[361]]))
                cols$Indexerr[wIyears8,"index8"]      <- na.omit(an(res1[[362]]))
                cols$Indexerr[wIyears9,"index9"]      <- na.omit(an(res1[[363]]))
                cols$Inumageyears[] <- na.omit(an(unlist(res1[364:(363+cols$Inum)])))

                      Iyearage1       <- na.omit(an(res1[[373]]));  wIyearage1 <- pmatch(Iyearage1,cols$years[1]:cols$years[2])
                      Iyearage2       <- na.omit(an(res1[[374]]));  wIyearage2 <- pmatch(Iyearage2,cols$years[1]:cols$years[2])
                      #idxIyearage     <- which(cols$Inumageyears>0) hack for 2011
                      idxIyearage     <- c(1,4)

                cols$Iyearsage[wIyearage1,idxIyearage[1]]   <- Iyearage1
                cols$Iyearsage[wIyearage2,idxIyearage[2]]   <- Iyearage2
                cols$Iagesample[wIyearage1,idxIyearage[1]]  <- na.omit(an(res1[[375]]))
                cols$Iagesample[wIyearage2,idxIyearage[2]]  <- na.omit(an(res1[[376]]))
                cols$Ipropage[wIyearage1,,idxIyearage[1]]   <- matrix(na.omit(an(unlist(res1[377:(376+cols$Inumageyears[idxIyearage[1]])]))),ncol=11,
                                                                nrow=cols$Inumageyears[idxIyearage[1]],byrow=T)
                cols$Ipropage[wIyearage2,,idxIyearage[2]]   <- matrix(na.omit(an(unlist(res1[390:(389+cols$Inumageyears[idxIyearage[2]])]))),ncol=11,
                                                                nrow=cols$Inumageyears[idxIyearage[2]],byrow=T)
                cols$Iwtatage[]                       <- array(matrix(na.omit(an(unlist(res1[396:437]))),ncol=11,
                                                               nrow=42,byrow=T),dim=c(42,11,9))

                #-Population data
                cols$Pwtatage[]                       <- na.omit(an(res1[[774]]))
                cols$Pmatatage[]                      <- na.omit(an(res1[[775]]))
                cols$Pspwn                            <- na.omit(an(res1[[776]]))
                cols$Pageerr[]                        <- matrix(na.omit(an(unlist(res1[777:787]))),ncol=11,nrow=11,byrow=T)
             return(cols)}